<!doctype html><html lang=en-us dir=ltr><head><meta charset=UTF-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=description content="Docker # 教程手册 # 好用的使用教程
Install # 略过~ 😁
基础命令 # 打镜像和跑 # 打 # docker build -t server:v0 . -f server.dockerfile dockerignore # 如果您想在 Dockerfile 中将当前目录中的所有文件都复制到 Docker 镜像中，可以使用 Dockerfile 中的 COPY 指令。具体的语法如下：
COPY . /目标路径/ 其中. 表示当前目录，/目标路径/ 表示目标路径，这里可以是绝对路径或相对路径。 这将会将当前目录中的所有文件复制到镜像中的 /app/ 目录下。请注意，在这种情况下，所有文件和文件夹都会被复制到镜像中，包括隐藏文件和文件夹。如果您不想复制某些文件或文件夹，可以在运行 COPY 指令之前通过 .dockerignore 文件来指定忽略的文件和文件夹。
跑 # docker run -p 9080:80 --name server server:v0 指定启动命令 # sudo docker run -it -p 9080:80 --name nova_server nova_server:v0 sh -c &#34;echo hello&#34; 加入自定义网络 # --network {{network name}} 详见：[[#Network]]"><meta name=theme-color content="#FFFFFF"><meta name=color-scheme content="light dark"><meta property="og:title" content="Docker"><meta property="og:description" content="Docker # 教程手册 # 好用的使用教程
Install # 略过~ 😁
基础命令 # 打镜像和跑 # 打 # docker build -t server:v0 . -f server.dockerfile dockerignore # 如果您想在 Dockerfile 中将当前目录中的所有文件都复制到 Docker 镜像中，可以使用 Dockerfile 中的 COPY 指令。具体的语法如下：
COPY . /目标路径/ 其中. 表示当前目录，/目标路径/ 表示目标路径，这里可以是绝对路径或相对路径。 这将会将当前目录中的所有文件复制到镜像中的 /app/ 目录下。请注意，在这种情况下，所有文件和文件夹都会被复制到镜像中，包括隐藏文件和文件夹。如果您不想复制某些文件或文件夹，可以在运行 COPY 指令之前通过 .dockerignore 文件来指定忽略的文件和文件夹。
跑 # docker run -p 9080:80 --name server server:v0 指定启动命令 # sudo docker run -it -p 9080:80 --name nova_server nova_server:v0 sh -c &#34;echo hello&#34; 加入自定义网络 # --network {{network name}} 详见：[[#Network]]"><meta property="og:type" content="article"><meta property="og:url" content="http://example.org/docs/programmer/cloudnative/%E5%AE%B9%E5%99%A8/note-of-docker/"><meta property="article:section" content="docs"><meta property="article:published_time" content="2021-12-16T16:00:00+00:00"><meta property="article:modified_time" content="2021-12-16T16:00:00+00:00"><title>Docker | Ian's Blog</title>
<link rel=manifest href=/manifest.json><link rel=icon href=/favicon.png type=image/x-icon><link rel=stylesheet href=/book.min.c58292d36b18b675680ab9baea2029204537b839ea72f258746ec0f32ce8d6c8.css integrity="sha256-xYKS02sYtnVoCrm66iApIEU3uDnqcvJYdG7A8yzo1sg=" crossorigin=anonymous><script defer src=/flexsearch.min.js></script><script defer src=/en.search.min.80b0c12c6a3982c23bab5eb1d10c75b4cf8bfbaa1c7e183729648ce8a09207d7.js integrity="sha256-gLDBLGo5gsI7q16x0Qx1tM+L+6ocfhg3KWSM6KCSB9c=" crossorigin=anonymous></script></head><body dir=ltr><input type=checkbox class="hidden toggle" id=menu-control>
<input type=checkbox class="hidden toggle" id=toc-control><main class="container flex"><aside class=book-menu><div class=book-menu-content><nav><h2 class=book-brand><a class="flex align-center" href=/><span>Ian's Blog</span></a></h2><div class=book-search><input type=text id=book-search-input placeholder=Search aria-label=Search maxlength=64 data-hotkeys=s/><div class="book-search-spinner hidden"></div><ul id=book-search-results></ul></div><ul><li class=book-section-flat><a href=/docs/programmer/>程序员笔记</a><ul><li><input type=checkbox id=section-d5f99046a51e5e750b61f2e037945fcc class=toggle>
<label for=section-d5f99046a51e5e750b61f2e037945fcc class="flex justify-between"><a role=button>基础工具和配置</a></label><ul><li><a href=/docs/programmer/basetc/%E6%96%87%E6%9C%AC%E4%B8%89%E5%89%91%E5%AE%A2/>文本三剑客</a></li><li><a href=/docs/programmer/basetc/tmux/>Tmux使用笔记</a></li><li><a href=/docs/programmer/basetc/obsidian%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE%E6%8F%92%E4%BB%B6/>Obsidian思维导图插件</a></li><li><a href=/docs/programmer/basetc/for_china/>各个软件换源</a></li><li><a href=/docs/programmer/basetc/tipsofvim/>tip Of vim</a></li><li><a href=/docs/programmer/basetc/editer/>编辑器使用</a></li><li><a href=/docs/programmer/basetc/bash/>Bash</a></li><li><a href=/docs/programmer/basetc/gitbook/>Gitbook</a></li><li><a href=/docs/programmer/basetc/vim/>Vim</a></li><li><a href=/docs/programmer/basetc/%E5%AE%88%E6%8A%A4%E8%BF%9B%E7%A8%8B/>supervisor</a></li></ul></li><li><input type=checkbox id=section-8658298e10b544e890095f646916165a class=toggle checked>
<label for=section-8658298e10b544e890095f646916165a class="flex justify-between"><a href=/docs/programmer/cloudnative/>云原生</a></label><ul><li><input type=checkbox id=section-31d3b02fd4a132635e483e7e756058ea class=toggle>
<label for=section-31d3b02fd4a132635e483e7e756058ea class="flex justify-between"><a href=/docs/programmer/cloudnative/redis/>中间件</a></label><ul><li><a href=/docs/programmer/cloudnative/redis/python-redis-%E5%AE%A2%E6%88%B7%E7%AB%AF/>Python Redis 客户端</a></li><li><a href=/docs/programmer/cloudnative/redis/bigkey-and-hotkey/>大key、热key问题</a></li><li><a href=/docs/programmer/cloudnative/redis/the-basics-of-redis/>Redis基础</a></li><li><a href=/docs/programmer/cloudnative/redis/advanced-knowledge-of-redis/>Redis进阶</a></li><li><a href=/docs/programmer/cloudnative/redis/%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4/>Redis常用命令</a></li></ul></li><li><input type=checkbox id=section-62d608ed890b3abc76dae78ccfcab912 class=toggle>
<label for=section-62d608ed890b3abc76dae78ccfcab912 class="flex justify-between"><a role=button>k8s</a></label><ul><li><a href=/docs/programmer/cloudnative/k8s/elk%E5%9C%A8k8s%E4%B8%8A%E7%9A%84%E9%83%A8%E7%BD%B2%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B/>elk在k8s上的部署使用示例</a></li><li><a href=/docs/programmer/cloudnative/k8s/k8s-%E9%85%8D%E5%A5%97%E8%AF%B4%E6%98%8E/>k8s 配套说明</a></li><li><a href=/docs/programmer/cloudnative/k8s/k8s-%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/>k8s技术分享</a></li><li><a href=/docs/programmer/cloudnative/k8s/k8s%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E5%92%8C%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%A7%A3%E6%9E%90/>k8s学习-常用命令和配置文件</a></li><li><a href=/docs/programmer/cloudnative/k8s/argo-workflow%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95%E5%92%8C%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF%E5%88%86%E6%9E%90/>Argo Workflow性能测试和使用场景分析</a></li><li><a href=/docs/programmer/cloudnative/k8s/argo-%E4%BD%BF%E7%94%A8%E8%AE%B0%E5%BD%95/>Argo 使用记录</a></li></ul></li><li><input type=checkbox id=section-b828bf3d116bc282da9db25a06bf908e class=toggle>
<label for=section-b828bf3d116bc282da9db25a06bf908e class="flex justify-between"><a role=button>中间件</a></label><ul><li><a href=/docs/programmer/cloudnative/middleware/kafka-%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/>Kafka 安装和使用</a></li></ul></li><li><a href=/docs/programmer/cloudnative/%E5%AE%B9%E5%99%A8/note-of-podman/>Podman</a></li><li><a href=/docs/programmer/cloudnative/nginx%E5%AE%9E%E7%94%A8%E9%85%8D%E7%BD%AE/>Nginx实用配置</a></li><li><a href=/docs/programmer/cloudnative/uwsgi-%E5%A4%84%E7%90%86%E8%AE%B0%E5%BD%95/>uwsgi 处理记录</a></li><li><a href=/docs/programmer/cloudnative/note-of-docker/>Docker</a></li><li><a href=/docs/programmer/cloudnative/%E5%AE%B9%E5%99%A8/note-of-docker/ class=active>Docker</a></li><li><a href=/docs/programmer/cloudnative/fastapi/>Django的建站的(｡･･)ﾉﾞ</a></li><li><a href=/docs/programmer/cloudnative/nginx-%E9%AB%98%E5%8F%AF%E7%94%A8/>Nginx高可用</a></li><li><a href=/docs/programmer/cloudnative/notesdjango/>Django的建站的(｡･･)ﾉﾞ</a></li><li><a href=/docs/programmer/cloudnative/sonar-%E4%BB%A3%E7%A0%81%E9%9D%99%E6%80%81%E6%A3%80%E6%9F%A5/>Sonar 代码静态检查</a></li></ul></li><li><input type=checkbox id=section-883e27361d38e16afb68faff3435ac0b class=toggle>
<label for=section-883e27361d38e16afb68faff3435ac0b class="flex justify-between"><a role=button>机器学习</a></label><ul><li><a href=/docs/programmer/ml/stable-diffusion/>AI画图</a></li><li><a href=/docs/programmer/ml/%E7%88%AC%E8%99%AB/>爬虫</a></li><li><a href=/docs/programmer/ml/paddle/>Paddle</a></li><li><a href=/docs/programmer/ml/tensorflow/>Tensorflow</a></li><li><a href=/docs/programmer/ml/opencv/>OpenCV</a></li><li><a href=/docs/programmer/ml/yolo/>Demo Test项目中的一些东西</a></li><li><a href=/docs/programmer/ml/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%B8%B8%E7%94%A8%E5%BA%93%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95/>机器学习库</a></li></ul></li><li><input type=checkbox id=section-0bf4f4329214e20fa67ca3e12c6aad0c class=toggle>
<label for=section-0bf4f4329214e20fa67ca3e12c6aad0c class="flex justify-between"><a role=button>平台</a></label><ul><li><a href=/docs/programmer/platforms/ubuntu/>Ubuntu</a></li><li><a href=/docs/programmer/platforms/%E9%98%BF%E9%87%8C%E4%BA%91%E4%BD%BF%E7%94%A8/>阿里云使用</a></li><li><a href=/docs/programmer/platforms/wps-for-linux/>WPS for Linux</a></li><li><a href=/docs/programmer/platforms/appsflyer/>AppsFlyer-外网移动归因营销分析平台</a></li></ul></li><li><input type=checkbox id=section-bf4e0d6f0b81f7b3ec08ed1fc66b874d class=toggle>
<label for=section-bf4e0d6f0b81f7b3ec08ed1fc66b874d class="flex justify-between"><a href=/docs/programmer/langs/>编程语言</a></label><ul><li><input type=checkbox id=section-771df6c720301e69f1715f7fc174ac3d class=toggle>
<label for=section-771df6c720301e69f1715f7fc174ac3d class="flex justify-between"><a role=button>Python</a></label><ul><li><a href=/docs/programmer/langs/python/sqlalchemy/>SqlAlchemy - 数据库Orm</a></li><li><a href=/docs/programmer/langs/python/pypi/>PyPi使用说明</a></li><li><a href=/docs/programmer/langs/python/pytest/>PyTest</a></li><li><a href=/docs/programmer/langs/python/paramiko-%E4%BD%BF%E7%94%A8-sshsftp/>Paramiko 使用 Ssh&amp;sftp</a></li><li><a href=/docs/programmer/langs/python/%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%A4%9A%E8%BF%9B%E7%A8%8B/>Python 多线程多进程</a></li><li><a href=/docs/programmer/langs/python/notespython/>Python 常用库</a></li><li><a href=/docs/programmer/langs/python/notespython/>Python 笔记</a></li><li><a href=/docs/programmer/langs/python/py%E5%B0%8F%E5%B7%A5%E5%85%B7%E5%92%8C%E5%8A%9F%E8%83%BD%E6%80%A7%E6%96%B9%E6%B3%95/>Py小工具和功能性方法</a></li><li><a href=/docs/programmer/langs/python/notespython/>解决问题</a></li></ul></li><li><input type=checkbox id=section-9f8ac8f06e138c7ac13ff61f23b4d497 class=toggle>
<label for=section-9f8ac8f06e138c7ac13ff61f23b4d497 class="flex justify-between"><a role=button>Golang</a></label><ul><li><a href=/docs/programmer/langs/golang/advanced-knowledge-of-golang/>Golang进阶笔记</a></li><li><a href=/docs/programmer/langs/golang/noteofgoexp/>Golang进阶笔记</a></li><li><a href=/docs/programmer/langs/golang/note-of-golang/>Golang笔记</a></li><li><a href=/docs/programmer/langs/golang/noteofgolang/>Golang笔记</a></li></ul></li><li><a href=/docs/programmer/langs/cmake/>CMake 使用Tips</a></li><li><a href=/docs/programmer/langs/tips-of-debugers/>Tips of debuggers</a></li><li><a href=/docs/programmer/langs/tips-of-markdown/>Tips of MarkDown</a></li><li><a href=/docs/programmer/langs/java/notesjava/>愉快的Java(happy to learn the fuck java)</a></li><li><a href=/docs/programmer/langs/note-for-fmtdata/>数据格式笔记</a></li></ul></li><li><input type=checkbox id=section-de7bfad1d124522974cdf8addfbb23f2 class=toggle>
<label for=section-de7bfad1d124522974cdf8addfbb23f2 class="flex justify-between"><a role=button>Net</a></label><ul><li><a href=/docs/programmer/net/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/>网络编程</a></li><li><a href=/docs/programmer/net/nginx%E5%AE%9E%E7%94%A8%E9%85%8D%E7%BD%AE/>Nginx实用配置</a></li><li><a href=/docs/programmer/net/tips-of-grpc/>gRpc使用小记</a></li><li><a href=/docs/programmer/net/epoll%E5%AE%9E%E7%8E%B0/>Epoll实现</a></li><li><a href=/docs/programmer/net/net/>Epoll实现</a></li></ul></li><li><input type=checkbox id=section-4446dd07527142b855f26d7cc8f0e617 class=toggle>
<label for=section-4446dd07527142b855f26d7cc8f0e617 class="flex justify-between"><a href=/docs/programmer/database/>Database</a></label><ul><li><a href=/docs/programmer/database/mongodb/>Mongodb使用记录</a></li><li><a href=/docs/programmer/database/dgraph/>Dgraph使用小记</a></li><li><a href=/docs/programmer/database/note-of-db-data-mongodb/>数据库-MongoDB篇</a></li><li><a href=/docs/programmer/database/note-of-db-data/>数据库-MongoDB篇</a></li><li><a href=/docs/programmer/database/note-of-db-data-mysql/>数据库-MySQL篇</a></li></ul></li><li><input type=checkbox id=section-7e5360c5e7954906b897ed79085884b6 class=toggle>
<label for=section-7e5360c5e7954906b897ed79085884b6 class="flex justify-between"><a href=/docs/programmer/gui/>图形用户界面-GUI</a></label><ul><li><a href=/docs/programmer/gui/pyinstaller-python%E6%89%93%E5%8C%85/>python打包</a></li><li><a href=/docs/programmer/gui/pyinstaller/>python打包</a></li><li><a href=/docs/programmer/gui/qt/>Qt/PySide</a></li><li><a href=/docs/programmer/gui/noteofvn_py/>Vn.Py学习笔记（Python交易平台框架）</a></li><li><a href=/docs/programmer/gui/vn.py%E7%AC%94%E8%AE%B0-%E4%BA%A4%E6%98%93%E5%B9%B3%E5%8F%B0%E5%AE%A2%E6%88%B7%E7%AB%AF/>Vn.Py学习笔记（Python交易平台框架）</a></li><li><a href=/docs/programmer/gui/notespython/>图形化界面 （Python Gui）</a></li><li><a href=/docs/programmer/gui/notespython/>图形化界面 （Python Gui）</a></li></ul></li><li><input type=checkbox id=section-ddcbe632dc99a9fb372422dada8ee641 class=toggle>
<label for=section-ddcbe632dc99a9fb372422dada8ee641 class="flex justify-between"><a role=button>OS操作系统问题处理</a></label><ul><li><a href=/docs/programmer/os/install_some/>安装问题</a></li><li><a href=/docs/programmer/os/android/>安卓</a></li><li><a href=/docs/programmer/os/freebsd/>FreeBSD</a></li><li><a href=/docs/programmer/os/note-of-linux/>Linux 笔记</a></li><li><a href=/docs/programmer/os/npm/>Npm</a></li><li><a href=/docs/programmer/os/git/>Git</a></li><li><a href=/docs/programmer/os/problem-of-windows/>Windows 爬坑记</a></li><li><a href=/docs/programmer/os/tips-of-problems/>解决问题记录笔记</a></li></ul></li><li><input type=checkbox id=section-d325c59fc6513e1b1e05a60b192d4973 class=toggle>
<label for=section-d325c59fc6513e1b1e05a60b192d4973 class="flex justify-between"><a href=/docs/programmer/hardware/>硬件</a></label><ul><li><a href=/docs/programmer/hardware/raspberrypi/>Raspberry Pi</a></li><li><a href=/docs/programmer/hardware/screen/>Screen</a></li></ul></li></ul></li><li class=book-section-flat><span>建模和游戏</span><ul><li><a href=/docs/3dgame/blender/>Blender</a></li><li><a href=/docs/3dgame/noteofue4/>UE4 笔记</a></li></ul></li><li class=book-section-flat><a href=/docs/example/>Hugo特殊Markdown语法说明</a><ul><li><a href=/docs/example/table-of-contents/>Table of Contents</a><ul><li><a href=/docs/example/table-of-contents/with-toc/>With ToC</a></li><li><a href=/docs/example/table-of-contents/without-toc/>Without ToC</a></li></ul></li><li><a href=/docs/example/shortcodes/buttons/>Buttons</a></li><li><a href=/docs/example/shortcodes/columns/>Columns</a></li><li><a href=/docs/example/shortcodes/details/>Details</a></li><li><a href=/docs/example/shortcodes/expand/>Expand</a></li><li><a href=/docs/example/shortcodes/hints/>Hints</a></li><li><a href=/docs/example/shortcodes/tabs/>Tabs</a></li></ul></li></ul><ul><li><a href=/posts/>Blog</a></li></ul></nav><script>(function(){var e=document.querySelector("aside .book-menu-content");addEventListener("beforeunload",function(){localStorage.setItem("menu.scrollTop",e.scrollTop)}),e.scrollTop=localStorage.getItem("menu.scrollTop")})()</script></div></aside><div class=book-page><header class=book-header><div class="flex align-center justify-between"><label for=menu-control><img src=/svg/menu.svg class=book-icon alt=Menu>
</label><strong>Docker</strong>
<label for=toc-control><img src=/svg/toc.svg class=book-icon alt="Table of Contents"></label></div><aside class="hidden clearfix"><nav id=TableOfContents><ul><li><a href=#教程手册>教程手册</a><ul><li><a href=#install>Install</a></li></ul></li><li><a href=#基础命令>基础命令</a><ul><li><a href=#打镜像和跑>打镜像和跑</a></li><li><a href=#打>打</a></li><li><a href=#跑>跑</a></li><li><a href=#重命名>重命名</a></li></ul></li><li><a href=#network>Network</a><ul><li><a href=#删除>删除</a></li></ul></li><li><a href=#lazydocker-可视化>lazydocker 可视化</a></li></ul><ul><li><a href=#用户sudo问题>用户sudo问题</a><ul><li><a href=#不好使的话>不好使的话</a></li><li><a href=#docker-mysql编码>Docker Mysql编码</a></li><li><a href=#时区修改-镜像时间问题>时区修改-镜像时间问题</a></li><li><a href=#mysql-时区问题>Mysql 时区问题</a></li></ul></li><li><a href=#bash进入某app内>bash进入某App内</a></li><li><a href=#swarm>Swarm</a><ul><li></li></ul></li><li><a href=#空间占用问题>空间占用问题</a><ul><li><a href=#通过snap安装的docker配置文件路径>通过snap安装的docker配置文件路径</a></li></ul></li><li><a href=#连接>连接</a><ul><li></li></ul></li></ul><ul><li><a href=#示例>示例</a><ul><li><a href=#dockerfile>Dockerfile</a></li><li><a href=#k8s-yaml>k8s yaml</a></li></ul></li></ul></nav></aside></header><article class=markdown><h1 id=docker>Docker
<a class=anchor href=#docker>#</a></h1><p><img src="https://mma.prnewswire.com/media/776689/New_Docker_logo_Logo.jpg?p=facebook" alt="docker img" title=docker></p><h2 id=教程手册>教程手册
<a class=anchor href=#%e6%95%99%e7%a8%8b%e6%89%8b%e5%86%8c>#</a></h2><p><a href=https://yeasy.gitbook.io/docker_practice/introduction/why>好用的使用教程</a></p><h3 id=install>Install
<a class=anchor href=#install>#</a></h3><p>略过~ 😁</p><h2 id=基础命令>基础命令
<a class=anchor href=#%e5%9f%ba%e7%a1%80%e5%91%bd%e4%bb%a4>#</a></h2><h3 id=打镜像和跑>打镜像和跑
<a class=anchor href=#%e6%89%93%e9%95%9c%e5%83%8f%e5%92%8c%e8%b7%91>#</a></h3><h3 id=打>打
<a class=anchor href=#%e6%89%93>#</a></h3><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>docker build -t server:v0 . -f server.dockerfile
</span></span></code></pre></div><h4 id=dockerignore>dockerignore
<a class=anchor href=#dockerignore>#</a></h4><p>如果您想在 Dockerfile 中将当前目录中的所有文件都复制到 Docker 镜像中，可以使用 Dockerfile 中的 <code>COPY</code> 指令。具体的语法如下：</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Dockerfile data-lang=Dockerfile><span style=display:flex><span><span style=color:#66d9ef>COPY</span> . /目标路径/<span style=color:#960050;background-color:#1e0010>
</span></span></span></code></pre></div><p>其中<code>.</code> 表示当前目录，<code>/目标路径/</code> 表示目标路径，这里可以是绝对路径或相对路径。
这将会将当前目录中的所有文件复制到镜像中的 <code>/app/</code> 目录下。请注意，在这种情况下，所有文件和文件夹都会被复制到镜像中，包括隐藏文件和文件夹。如果您不想复制某些文件或文件夹，可以在运行 <code>COPY</code> 指令之前通过 <code>.dockerignore</code> 文件来指定忽略的文件和文件夹。</p><h3 id=跑>跑
<a class=anchor href=#%e8%b7%91>#</a></h3><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>docker run -p 9080:80 --name server server:v0
</span></span></code></pre></div><h4 id=指定启动命令>指定启动命令
<a class=anchor href=#%e6%8c%87%e5%ae%9a%e5%90%af%e5%8a%a8%e5%91%bd%e4%bb%a4>#</a></h4><pre tabindex=0><code>sudo docker run -it -p 9080:80 --name nova_server nova_server:v0 sh -c &#34;echo hello&#34;
</code></pre><h4 id=加入自定义网络>加入自定义网络
<a class=anchor href=#%e5%8a%a0%e5%85%a5%e8%87%aa%e5%ae%9a%e4%b9%89%e7%bd%91%e7%bb%9c>#</a></h4><p><code>--network {{network name}}</code>
详见：[[#Network]]</p><h4 id=跑出问题-进入容器排查>跑出问题-进入容器排查
<a class=anchor href=#%e8%b7%91%e5%87%ba%e9%97%ae%e9%a2%98-%e8%bf%9b%e5%85%a5%e5%ae%b9%e5%99%a8%e6%8e%92%e6%9f%a5>#</a></h4><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>sudo docker run -it -p 9080:80 --name nova_server nova_server:v0 sh -c <span style=color:#e6db74>&#34;bash&#34;</span>
</span></span></code></pre></div><p>使用<code>-it</code>进入容器手动执行命令进行调试</p><h4 id=启动全部容器>启动全部容器
<a class=anchor href=#%e5%90%af%e5%8a%a8%e5%85%a8%e9%83%a8%e5%ae%b9%e5%99%a8>#</a></h4><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>docker ps -a | grep <span style=color:#e6db74>&#34;Exited&#34;</span> | awk <span style=color:#e6db74>&#39;{print $1}&#39;</span> | xargs docker start
</span></span></code></pre></div><h3 id=重命名>重命名
<a class=anchor href=#%e9%87%8d%e5%91%bd%e5%90%8d>#</a></h3><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>podman rename <span style=color:#f92672>{</span>old_name<span style=color:#f92672>}</span> <span style=color:#f92672>{</span>new_name<span style=color:#f92672>}</span>
</span></span></code></pre></div><h2 id=network>Network
<a class=anchor href=#network>#</a></h2><ol><li>docker run时容器加入方法-redis为例</li><li>后期加入方式</li><li>创建一个新网</li><li>检测</li></ol><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-makefile data-lang=makefile><span style=display:flex><span>    init_redis:
</span></span><span style=display:flex><span>    	docker run -p 6379:6379 <span style=color:#ae81ff>\\</span>
</span></span><span style=display:flex><span>    	--network websafe-network <span style=color:#ae81ff>\\</span>
</span></span><span style=display:flex><span>	    	--name redislatest redis:latest --requirepass 3b21d032a7fd8eb51fa7bd8a2e812b78b7b3a36f 
</span></span><span style=display:flex><span>    
</span></span><span style=display:flex><span>    join_network:
</span></span><span style=display:flex><span>    	docker network connect websafe-network mysql56
</span></span><span style=display:flex><span>    	docker network connect websafe-network daemon-xxx
</span></span><span style=display:flex><span>    
</span></span><span style=display:flex><span>    init_network:
</span></span><span style=display:flex><span>    	docker network create websafe-network
</span></span><span style=display:flex><span>    
</span></span><span style=display:flex><span>    check_network:
</span></span><span style=display:flex><span>    	docker network inspect websafe-network
</span></span><span style=display:flex><span>    	docker network inspect websafe-network | findStr IPv4Address
</span></span><span style=display:flex><span>    
</span></span></code></pre></div><h3 id=删除>删除
<a class=anchor href=#%e5%88%a0%e9%99%a4>#</a></h3><h2 id=lazydocker-可视化>lazydocker 可视化
<a class=anchor href=#lazydocker-%e5%8f%af%e8%a7%86%e5%8c%96>#</a></h2><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>go install github.com/jesseduffield/lazydocker@latest
</span></span></code></pre></div><blockquote><p>如果需要<code>sudo</code>权限的话无法直接执行命令，只能where获取原始路径后<code>sudo</code>+原始路径执行
如：</p></blockquote><ul><li>✓<code>sudo /home/ian/go/bin/lazydocker</code></li><li>✗<code>sudo lazydocker</code></li></ul><h1 id=常见问题和模板>常见问题和模板
<a class=anchor href=#%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98%e5%92%8c%e6%a8%a1%e6%9d%bf>#</a></h1><h2 id=用户sudo问题>用户sudo问题
<a class=anchor href=#%e7%94%a8%e6%88%b7sudo%e9%97%ae%e9%a2%98>#</a></h2><p><a href=https://ianvzs.github.io/2016/10/08/makeThingsEasyAndEnjoy/>debian增加docker用户组,优化每次sudo问题</a>
或</p><pre tabindex=0><code>sudo groupadd docker # 安装完docker.io之后一般都会自动创建所以这一步其实没啥用
sudo gpasswd -a $USERNAME docker
newgrp docker # 更新
</code></pre><h3 id=不好使的话>不好使的话
<a class=anchor href=#%e4%b8%8d%e5%a5%bd%e4%bd%bf%e7%9a%84%e8%af%9d>#</a></h3><blockquote><p>这里我有个docker(Docker version 24.0.5, build ced0996)</p></blockquote><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>docker --version
</span></span><span style=display:flex><span>sudo groupadd docker
</span></span><span style=display:flex><span>sudo gpasswd -a ian docker
</span></span><span style=display:flex><span>sudo systemctl status snap.docker.dockerd.service
</span></span><span style=display:flex><span>sudo systemctl restart snap.docker.dockerd.service
</span></span><span style=display:flex><span>sudo systemctl status snap.docker.dockerd.service
</span></span><span style=display:flex><span>sudo usermod -aG docker ian
</span></span><span style=display:flex><span>sudo chmod <span style=color:#ae81ff>666</span> /var/run/docker.sock
</span></span><span style=display:flex><span>sudo setfacl --modify user:ian:rw /var/run/docker.sock
</span></span><span style=display:flex><span>docker ps -a
</span></span></code></pre></div><h3 id=docker-mysql编码>Docker Mysql编码
<a class=anchor href=#docker-mysql%e7%bc%96%e7%a0%81>#</a></h3><pre tabindex=0><code class=language-docker-compose data-lang=docker-compose>version: &#34;2.2&#34;
services:
  redis:
    image: &#34;redis&#34;
    # ports:
    #  - 6379:6379
    command: redis-server --appendonly yes #一个容器启动时要运行的命令
    restart: always # 自动重启
  myserver:
    image: mainName/myserver
    restart: always # 自动重启
  mainServer:
    image: mainName/mainServer
    #    container_name: mainServerv1.0.0
    depends_on:
      - redis
    ports:
      - 9001:9001
    restart: always
    volumes:
      - &#34;./logs:/src/build/logs&#34;
    links:
      - redis
      - myserver
</code></pre><h4 id=mysql-编码问题单条><strong>mysql 编码问题|单条</strong>
<a class=anchor href=#mysql-%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e5%8d%95%e6%9d%a1>#</a></h4><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>docker run --name predix_mysql -e MYSQL_ROOT_PASSWORD<span style=color:#f92672>=</span>predix123predix -p 33061:3306  -e LANG<span style=color:#f92672>=</span>C.UTF-8 -e TZ<span style=color:#f92672>=</span>Asia/Shanghai -d mysql:5.7 --character-set-server<span style=color:#f92672>=</span>utf8mb4 --collation-server<span style=color:#f92672>=</span>utf8mb4_general_ci<span style=color:#e6db74>`</span>
</span></span></code></pre></div><h3 id=时区修改-镜像时间问题>时区修改-镜像时间问题
<a class=anchor href=#%e6%97%b6%e5%8c%ba%e4%bf%ae%e6%94%b9-%e9%95%9c%e5%83%8f%e6%97%b6%e9%97%b4%e9%97%ae%e9%a2%98>#</a></h3><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Dockerfile data-lang=Dockerfile><span style=display:flex><span><span style=color:#66d9ef>FROM</span><span style=color:#e6db74> alpine:3.14</span><span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010></span><span style=color:#66d9ef>RUN</span> apk add -U tzdata <span style=color:#f92672>&amp;&amp;</span> cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime <span style=color:#f92672>&amp;&amp;</span>  echo <span style=color:#e6db74>&#34;Asia/Shanghai&#34;</span> &gt; /etc/timezone<span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010></span><span style=color:#66d9ef>ENV</span> TZ<span style=color:#f92672>=</span>Asia/Shanghai <span style=color:#ae81ff>\
</span></span></span><span style=display:flex><span><span style=color:#ae81ff></span>    GO111MODULE<span style=color:#f92672>=</span>on <span style=color:#ae81ff>\
</span></span></span><span style=display:flex><span><span style=color:#ae81ff></span>    CGO_ENABLED<span style=color:#f92672>=</span><span style=color:#ae81ff>0</span> <span style=color:#ae81ff>\
</span></span></span><span style=display:flex><span><span style=color:#ae81ff></span>    GOOS<span style=color:#f92672>=</span>linux <span style=color:#ae81ff>\
</span></span></span><span style=display:flex><span><span style=color:#ae81ff></span>    GOARCH<span style=color:#f92672>=</span>amd64 <span style=color:#ae81ff>\
</span></span></span><span style=display:flex><span><span style=color:#ae81ff></span>    PROGRAM_ENV<span style=color:#f92672>=</span>pro<span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010></span><span style=color:#66d9ef>WORKDIR</span><span style=color:#e6db74> /src/build</span><span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010></span><span style=color:#75715e># 复制构建应用程序所需的代码</span><span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010></span><span style=color:#66d9ef>COPY</span> ./build .<span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010></span><span style=color:#66d9ef>EXPOSE</span><span style=color:#e6db74> 8088</span><span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010>
</span></span></span><span style=display:flex><span><span style=color:#960050;background-color:#1e0010></span><span style=color:#66d9ef>CMD</span> [<span style=color:#e6db74>&#34;./main&#34;</span>]<span style=color:#960050;background-color:#1e0010>
</span></span></span></code></pre></div><h3 id=mysql-时区问题>Mysql 时区问题
<a class=anchor href=#mysql-%e6%97%b6%e5%8c%ba%e9%97%ae%e9%a2%98>#</a></h3><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Dockerfile data-lang=Dockerfile><span style=display:flex><span>-e TZ<span style=color:#f92672>=</span>Asia/Shanghai<span style=color:#960050;background-color:#1e0010>
</span></span></span></code></pre></div><h2 id=bash进入某app内>bash进入某App内
<a class=anchor href=#bash%e8%bf%9b%e5%85%a5%e6%9f%90app%e5%86%85>#</a></h2><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>docker exec -it webserver bash
</span></span></code></pre></div><p><code>webserver</code> 为APP名称</p><h2 id=swarm>Swarm
<a class=anchor href=#swarm>#</a></h2><p>Docker Compose
Docker Machine: Mac、Windows区别预先安装。 Linux直接安装 Win10 Hyper-V
发布镜像
镜像充当已部署容器，填写必要信息：username、repo、tag
dockers-compose.yml 副本</p><p>其实以上在官方教程有很好说明<a href=https://docs.docker-cn.com/get-started/part4/#%E5%85%88%E5%86%B3%E6%9D%A1%E4%BB%B6>在这里</a>，但毕竟高级应用，暂且记下。</p><h4 id=创建虚拟机来试验一下>创建虚拟机来试验一下
<a class=anchor href=#%e5%88%9b%e5%bb%ba%e8%99%9a%e6%8b%9f%e6%9c%ba%e6%9d%a5%e8%af%95%e9%aa%8c%e4%b8%80%e4%b8%8b>#</a></h4><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-shell data-lang=shell><span style=display:flex><span>$ docker-machine create --driver virtualbox myvm1
</span></span><span style=display:flex><span>$ docker-machine create --driver virtualbox myvm2
</span></span></code></pre></div><p>第一个管理节点：<code>docker swarm init</code>，第二个工作节点`docker swarm join``</p><h5 id=should-like-this-show>should like this show:
<a class=anchor href=#should-like-this-show>#</a></h5><pre tabindex=0><code>$ docker-machine ssh myvm1 &#34;docker swarm init&#34;
Swarm initialized: current node &lt;node ID&gt; is now a manager.

To add a worker to this swarm, run the following command:

  docker swarm join \
  --token &lt;token&gt; \
  &lt;ip&gt;:&lt;port&gt;
</code></pre><p>通过运行 docker-machine ls 来复制 myvm1 的 IP 地址，然后使用 该 IP 地址并通过 &ndash;advertise-addr 指定端口 2377（用于 swarm join 的端口）， 以便再次运行 docker swarm init 命令。例如：</p><pre tabindex=0><code>    docker-machine ssh myvm1 &#34;docker swarm init --advertise-addr 192.168.99.100:2377&#34;
</code></pre><h5 id=复制此命令然后通过-docker-machine-ssh-将其发送给-myvm2从而让-myvm2-加入>复制此命令，然后通过 docker-machine ssh 将其发送给 myvm2，从而让 myvm2 加入
<a class=anchor href=#%e5%a4%8d%e5%88%b6%e6%ad%a4%e5%91%bd%e4%bb%a4%e7%84%b6%e5%90%8e%e9%80%9a%e8%bf%87-docker-machine-ssh-%e5%b0%86%e5%85%b6%e5%8f%91%e9%80%81%e7%bb%99-myvm2%e4%bb%8e%e8%80%8c%e8%ae%a9-myvm2-%e5%8a%a0%e5%85%a5>#</a></h5><pre tabindex=0><code>$ docker-machine ssh myvm2 &#34;docker swarm join \
--token &lt;token&gt; \
&lt;ip&gt;:&lt;port&gt;&#34;

This node joined a swarm as a worker.
</code></pre><p>至此， 创建swarm完成。</p><h2 id=空间占用问题>空间占用问题
<a class=anchor href=#%e7%a9%ba%e9%97%b4%e5%8d%a0%e7%94%a8%e9%97%ae%e9%a2%98>#</a></h2><ul><li>首先查看docker存储位置</li></ul><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>sudo docker info
</span></span><span style=display:flex><span><span style=color:#75715e># 这里: Docker Root Dir:</span>
</span></span></code></pre></div><p>修改<code>/etc/docker/deamon.json</code> or <code>~/.config/docker/daemon.json</code>:
<code>{ "data-root": "/home/hellxz/docker-home"}</code></p><h3 id=通过snap安装的docker配置文件路径>通过snap安装的docker配置文件路径
<a class=anchor href=#%e9%80%9a%e8%bf%87snap%e5%ae%89%e8%a3%85%e7%9a%84docker%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6%e8%b7%af%e5%be%84>#</a></h3><ul><li><code>/var/snap/docker/current/etc/docker/daemon.json</code></li><li><code>/var/snap/docker/current/config/daemon.json</code></li></ul><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-bash data-lang=bash><span style=display:flex><span>sudo snap restart docker
</span></span></code></pre></div><hr><h2 id=连接>连接
<a class=anchor href=#%e8%bf%9e%e6%8e%a5>#</a></h2><p>使用<code>ssh</code>连接<code>docker-machine ssh myvm1</code>,运行<code>docker node ls</code>查看此中节点。</p><h4 id=mybe-like-this>Mybe like this.
<a class=anchor href=#mybe-like-this>#</a></h4><pre tabindex=0><code>docker@myvm1:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
brtu9urxwfd5j0zrmkubhpkbd     myvm2               Ready               Active              
rihwohkh3ph38fhillhhb84sk *   myvm1               Ready               Active              Leader
</code></pre><p>mybe not you are ture. 😂</p><p>之后，其余东西z有用到再去官网查看吧。 记录与否，取决于俺。</p><h1 id=supervisord-禁用日志文件或使用logfiledevstdout>supervisord 禁用日志文件或使用logfile=/dev/stdout
<a class=anchor href=#supervisord-%e7%a6%81%e7%94%a8%e6%97%a5%e5%bf%97%e6%96%87%e4%bb%b6%e6%88%96%e4%bd%bf%e7%94%a8logfiledevstdout>#</a></h1><p>from: <a href=https://www.coder.work/article/100835>https://www.coder.work/article/100835</a>
标签 <a href="https://www.coder.work/blog?tag=docker" title=docker>docker</a> <a href="https://www.coder.work/blog?tag=supervisord" title=supervisord>supervisord</a></p><pre tabindex=0><code class=language-supervisor data-lang=supervisor>[supervisord]
nodaemon=true
logfile=/dev/stdout
pidfile=/var/run/supervisord.pid
childlogdir=/var/log/supervisor
</code></pre><p>当我这样做时，这个主管会崩溃，因为它无法在/dev/stdout 中寻找</p><p>如何禁用 supervisord 在我的 docker 容器中创建任何日志文件？</p><p><strong>最佳答案</strong></p><p>对于主主管，<code>nodaemon</code> 将导致日志转到 <code>stdout</code></p><pre tabindex=0><code class=language-supervisor data-lang=supervisor>[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
</code></pre><p>然后将每个托管进程的日志发送到标准输出文件描述符<code>/dev/fd/1</code></p><pre tabindex=0><code class=language-supervisor data-lang=supervisor>[program:x]
command=echo test
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true
</code></pre><p>或者，如果您希望将 stderr 保留在不同的流上:</p><pre tabindex=0><code class=language-supervisor data-lang=supervisor>[program:x]
command=echo test
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
stderr_logfile=/dev/fd/2
stderr_logfile_maxbytes=0
</code></pre><p>关于docker - supervisord 禁用日志文件或使用 logfile=/dev/stdout，我们在Stack Overflow上找到一个类似的问题： <a href=https://stackoverflow.com/questions/45645758/>https://stackoverflow.com/questions/45645758/</a></p><h1 id=sed报device-or-resource-busy错误的处理原因及方式>sed报Device or resource busy错误的处理原因及方式
<a class=anchor href=#sed%e6%8a%a5device-or-resource-busy%e9%94%99%e8%af%af%e7%9a%84%e5%a4%84%e7%90%86%e5%8e%9f%e5%9b%a0%e5%8f%8a%e6%96%b9%e5%bc%8f>#</a></h1><blockquote><p>转载
kuSorZ
博主文章分类：Linux
文章标签: docker | sed
文章分类: Docker
原文出处： <a href=https://www.cnblogs.com/xuxinkun/p/7116737.html>https://www.cnblogs.com/xuxinkun/p/7116737.html</a>错误现象</p></blockquote><p>在docker容器中想要修改/etc/resolv.conf中的namesever，使用sed命令进行执行时遇到错误：</p><p>/ # sed -i &rsquo;s/192.168.1.1/192.168.1.254/g&rsquo; /etc/resolv.conf
sed: can&rsquo;t move &lsquo;/etc/resolv.conf73UqmG&rsquo; to &lsquo;/etc/resolv.conf&rsquo;: Device or resource busy</p><ol><li></li><li></li></ol><p>但是可以通过vi/vim直接修改这个文件/etc/resolv.conf这个文件的内容。
问题原因</p><p>sed命令的实质并不是修改文件，而是产生一个新的文件替换原有的文件。这里我们做了一个实验。</p><p>我先创建了一个test.txt的文件，文件内容是123。然后我使用sed命令对文件内容进行了替换。再次查看test.txt。</p><p>/ # stat test.txt File: test.txt Size: 4 Blocks: 8 IO Block: 4096 regular fileDevice: fd28h/64808d Inode: 265 Links: 1Access: (0644/-rw-r&ndash;r&ndash;) Uid: ( 0/ root) Gid: ( 0/ root)Access: 2017-07-04 06:28:35.000000000Modify: 2017-07-04 06:28:17.000000000Change: 2017-07-04 06:29:03.000000000/ # cat test.txt 123/ # sed -i &rsquo;s/123/321/g&rsquo; test.txt/ # stat test.txt File: test.txt Size: 4 Blocks: 8 IO Block: 4096 regular fileDevice: fd28h/64808d Inode: 266 Links: 1Access: (0644/-rw-r&ndash;r&ndash;) Uid: ( 0/ root) Gid: ( 0/ root)Access: 2017-07-04 06:29:31.000000000Modify: 2017-07-04 06:29:31.000000000Change: 2017-07-04 06:29:31.000000000/ # cat test.txt321</p><ol><li></li></ol><p>可以看到文件内容被正确修改了，但是同时，文件的inode也修改了。说明了实质上是新生成的文件替换了原有的文件。但是vim/vi是在原文件基础上修改的，所以inode没有变化。</p><p>在docker中，/etc/resolv.conf是通过挂载入容器的。所以当你想去删除这个挂载文件，也就是挂载点时，自然就会报Device or resource busy。</p><p>这个跟是不是特权privilege没有关系。即使是privilege的容器，也会有这个问题。
/ # rm /etc/resolv.conf
rm: can&rsquo;t remove &lsquo;/etc/resolv.conf&rsquo;: Device or resource busy</p><ol><li></li><li></li></ol><p>其实不仅仅/etc/resolv.conf，还有/etc/hostname，/etc/hosts等文件都是通过挂载方式挂载到容器中来的。所以想要用sed对他们进行修改，都会遇到这样的问题。我们可以通过df -h查看容器内的挂载情况。</p><p>/ # df -h
Filesystem Size Used Available Use% Mounted on/dev/mapper/docker-253:2-807144231-37acfcd86387ddcbc52ef8dac69d919283fc5d9d8ab5f55fd23d1c782e3b1c70 10.0G 33.8M 10.0G 0% /tmpfs 15.4G 0 15.4G 0% /devtmpfs 15.4G 0 15.4G 0% /sys/fs/cgroup/dev/mapper/centos-home 212.1G 181.8G 30.3G 86% /run/secrets/dev/mapper/centos-home 212.1G 181.8G 30.3G 86% /dev/termination-log/dev/mapper/centos-home 212.1G 181.8G 30.3G 86% /etc/resolv.conf/dev/mapper/centos-home 212.1G 181.8G 30.3G 86% /etc/hostname/dev/mapper/centos-home 212.1G 181.8G 30.3G 86% /etc/hostsshm 64.0M 0 64.0M 0% /dev/shmtmpfs 15.4G 0 15.4G 0% /proc/kcoretmpfs 15.4G 0 15.4G 0% /proc/timer_stats</p><ol><li></li><li></li></ol><p>如何解决</p><p>使用vi固然可以，但是对于批量操作就不是很合适了。可以通过sed和echo的组合命令echo &ldquo;$(sed &rsquo;s/192.168.1.1/192.168.1.254/g&rsquo; /etc/resolv.conf)&rdquo; > /etc/resolv.conf 即可实现替换。</p><p>/ # cat /etc/resolv.conf
search default.svc.games.local svc.games.local games.localnameserver 192.168.1.1options ndots:5/ # echo &ldquo;$(sed &rsquo;s/192.168.1.1/192.168.1.254/g&rsquo; /etc/resolv.conf)&rdquo; > /etc/resolv.conf
/ # cat /etc/resolv.conf
search default.svc.games.local svc.games.local games.localnameserver 192.168.1.254options ndots:5</p><ol><li></li><li></li><li></li><li></li></ol><p>这里如果使用sed &rsquo;s/192.168.1.1/192.168.1.254/g&rsquo; /etc/resolv.conf > /etc/resolv.conf是无效的。最终会导致/etc/resolv.conf内容为空。</p><h2 id=示例>示例
<a class=anchor href=#%e7%a4%ba%e4%be%8b>#</a></h2><h3 id=dockerfile>Dockerfile
<a class=anchor href=#dockerfile>#</a></h3><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Dockerfile data-lang=Dockerfile><span style=display:flex><span><span style=color:#66d9ef>CMD</span> mkdir -p /var/log/scancenter <span style=color:#ae81ff>\
</span></span></span><span style=display:flex><span><span style=color:#ae81ff></span>    <span style=color:#f92672>&amp;&amp;</span> sed <span style=color:#e6db74>&#39;s/INFO\|ERROR/DEBUG/g&#39;</span> -i scancenter/logging.cfg <span style=color:#ae81ff>\
</span></span></span><span style=display:flex><span><span style=color:#ae81ff></span>    <span style=color:#f92672>&amp;&amp;</span> uwsgi /opt/disk2/var/www/scancenter/3rd/conf/uwsgi-docker/uwsgi.ini --log-master<span style=color:#960050;background-color:#1e0010>
</span></span></span></code></pre></div><h3 id=k8s-yaml>k8s yaml
<a class=anchor href=#k8s-yaml>#</a></h3><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-yaml data-lang=yaml><span style=display:flex><span>    <span style=color:#f92672>spec</span>:
</span></span><span style=display:flex><span>      <span style=color:#f92672>containers</span>:
</span></span><span style=display:flex><span>      - <span style=color:#f92672>command</span>:
</span></span><span style=display:flex><span>        - <span style=color:#ae81ff>/bin/sh</span>
</span></span><span style=display:flex><span>        - -<span style=color:#ae81ff>c</span>
</span></span><span style=display:flex><span>        - <span style=color:#ae81ff>mkdir -p /var/log/scancenter &amp;&amp;</span>
</span></span><span style=display:flex><span>          <span style=color:#ae81ff>echo -e &#34;10.120.16.12\tapi.s.com\n10.5.25.5\tpp.api.com&#34; &gt;&gt; /etc/hosts &amp;&amp;</span>
</span></span><span style=display:flex><span>          <span style=color:#ae81ff>echo &#34;$(sed &#39;s/^-e //g&#39; /etc/hosts)&#34; &gt; /etc/hosts &amp;&amp;</span>
</span></span><span style=display:flex><span>          <span style=color:#ae81ff>sed &#39;s/INFO\|ERROR/DEBUG/g&#39; -i scancenter/logging.cfg &amp;&amp;</span>
</span></span><span style=display:flex><span>          <span style=color:#ae81ff>uwsgi /opt/disk2/var/www/scancenter/3rd/conf/uwsgi-docker/uwsgi.ini</span>
</span></span><span style=display:flex><span>        <span style=color:#f92672>env</span>:
</span></span><span style=display:flex><span>        - <span style=color:#f92672>name</span>: <span style=color:#ae81ff>POD_IP_ADDRESS</span>
</span></span><span style=display:flex><span>          <span style=color:#f92672>valueFrom</span>:
</span></span><span style=display:flex><span>            <span style=color:#f92672>fieldRef</span>:
</span></span><span style=display:flex><span>              <span style=color:#f92672>apiVersion</span>: <span style=color:#ae81ff>v1</span>
</span></span><span style=display:flex><span>              <span style=color:#f92672>fieldPath</span>: <span style=color:#ae81ff>status.podIP</span>
</span></span></code></pre></div></article><footer class=book-footer><div class="flex flex-wrap justify-between"></div><script>(function(){function e(e){const t=window.getSelection(),n=document.createRange();n.selectNodeContents(e),t.removeAllRanges(),t.addRange(n)}document.querySelectorAll("pre code").forEach(t=>{t.addEventListener("click",function(){if(window.getSelection().toString())return;e(t.parentElement),navigator.clipboard&&navigator.clipboard.writeText(t.parentElement.textContent)})})})()</script></footer><div class=book-comments></div><label for=menu-control class="hidden book-menu-overlay"></label></div><aside class=book-toc><div class=book-toc-content><nav id=TableOfContents><ul><li><a href=#教程手册>教程手册</a><ul><li><a href=#install>Install</a></li></ul></li><li><a href=#基础命令>基础命令</a><ul><li><a href=#打镜像和跑>打镜像和跑</a></li><li><a href=#打>打</a></li><li><a href=#跑>跑</a></li><li><a href=#重命名>重命名</a></li></ul></li><li><a href=#network>Network</a><ul><li><a href=#删除>删除</a></li></ul></li><li><a href=#lazydocker-可视化>lazydocker 可视化</a></li></ul><ul><li><a href=#用户sudo问题>用户sudo问题</a><ul><li><a href=#不好使的话>不好使的话</a></li><li><a href=#docker-mysql编码>Docker Mysql编码</a></li><li><a href=#时区修改-镜像时间问题>时区修改-镜像时间问题</a></li><li><a href=#mysql-时区问题>Mysql 时区问题</a></li></ul></li><li><a href=#bash进入某app内>bash进入某App内</a></li><li><a href=#swarm>Swarm</a><ul><li></li></ul></li><li><a href=#空间占用问题>空间占用问题</a><ul><li><a href=#通过snap安装的docker配置文件路径>通过snap安装的docker配置文件路径</a></li></ul></li><li><a href=#连接>连接</a><ul><li></li></ul></li></ul><ul><li><a href=#示例>示例</a><ul><li><a href=#dockerfile>Dockerfile</a></li><li><a href=#k8s-yaml>k8s yaml</a></li></ul></li></ul></nav></div></aside></main></body></html>